﻿Imports System.Text
Imports System.IO

Public Class Config

  ''' <summary>
  ''' Маска для шифровки/расшифровки
  ''' </summary>
  Public Mask As String

  ''' <summary>
  ''' путь до файла конфигурации
  ''' </summary>
  Public CfgFilePath As String

  ''' <summary>
  ''' флаг загрузки параметров конфигурации
  ''' </summary>
  Public flLoadConfig As Boolean = False


  ''' <summary>
  ''' Считывание параметров конфигурации из зашифрованного файла
  ''' </summary>
  ''' <remarks></remarks>
  Public Sub ReadCfg()

    If File.Exists(CfgFilePath) = False Then
      UI.MsgErr("Ошибка чтения файла конфигурации")
      flLoadConfig = False
      Exit Sub
    End If

    Dim StrPar As String
    Dim LenStrPar As Integer
    Dim BegPos As Integer

    Dim fileData() As Byte

    Try

      Using fs As System.IO.FileStream = System.IO.File.OpenRead(CfgFilePath)
        fileData = New Byte(fs.Length - 1) {}
        fs.Read(fileData, 0, CInt(fs.Length))
      End Using

      BegPos = 0
      LenStrPar = 90 - fileData(fileData.Length - 1) - 1
      StrPar = ret(fileData, BegPos, LenStrPar, Mask)
      ServerSQL.DataSource = StrPar

      BegPos += LenStrPar + 1

      LenStrPar = 90 - fileData(BegPos) - 1
      BegPos += 1
      StrPar = ret(fileData, BegPos, LenStrPar, Mask)
      ServerSQL.database = StrPar

      BegPos += LenStrPar + 1

      LenStrPar = 90 - fileData(BegPos) - 1
      BegPos += 1
      StrPar = ret(fileData, BegPos, LenStrPar, Mask)
      ServerSQL.SERVERProvider = StrPar

      BegPos += LenStrPar + 1

      LenStrPar = 90 - fileData(BegPos) - 1
      BegPos += 1
      StrPar = ret(fileData, BegPos, LenStrPar, Mask)
      ServerSQL.uid = StrPar

      BegPos += LenStrPar + 1

      LenStrPar = 90 - fileData(BegPos) - 1
      BegPos += 1
      StrPar = ret(fileData, BegPos, LenStrPar, Mask)
      ServerSQL.pwd = StrPar

      BegPos += LenStrPar + 1

      LenStrPar = 90 - fileData(BegPos) - 1
      BegPos += 1
      StrPar = ret(fileData, BegPos, LenStrPar, Mask)
      ServerFTP.Adress = StrPar

      BegPos += LenStrPar + 1

      LenStrPar = 90 - fileData(BegPos) - 1
      BegPos += 1
      StrPar = ret(fileData, BegPos, LenStrPar, Mask)
      ServerFTP.Port = StrPar

      BegPos += LenStrPar + 1

      LenStrPar = 90 - fileData(BegPos) - 1
      BegPos += 1
      StrPar = ret(fileData, BegPos, LenStrPar, Mask)
      ServerFTP.UserNameWrite = StrPar

      BegPos += LenStrPar + 1

      LenStrPar = 90 - fileData(BegPos) - 1
      BegPos += 1
      StrPar = ret(fileData, BegPos, LenStrPar, Mask)
      ServerFTP.PasswordWrite = StrPar

      BegPos += LenStrPar + 1

      LenStrPar = 90 - fileData(BegPos) - 1
      BegPos += 1
      StrPar = ret(fileData, BegPos, LenStrPar, Mask)
      ServerFTP.UserNameRead = StrPar

      BegPos += LenStrPar + 1

      LenStrPar = 90 - fileData(BegPos) - 1
      BegPos += 1
      StrPar = ret(fileData, BegPos, LenStrPar, Mask)
      ServerFTP.PasswordRead = StrPar



      If BegPos < fileData.Length Then
        BegPos += LenStrPar + 1
        LenStrPar = 90 - fileData(BegPos) - 1
        BegPos += 1
        StrPar = ret(fileData, BegPos, LenStrPar, Mask)
        ServerSQLPCD.DataSource = StrPar

        BegPos += LenStrPar + 1
        LenStrPar = 90 - fileData(BegPos) - 1
        BegPos += 1
        StrPar = ret(fileData, BegPos, LenStrPar, Mask)
        ServerSQLPCD.database = StrPar

        BegPos += LenStrPar + 1
        LenStrPar = 90 - fileData(BegPos) - 1
        BegPos += 1
        StrPar = ret(fileData, BegPos, LenStrPar, Mask)
        ServerSQLPCD.SERVERProvider = StrPar

        BegPos += LenStrPar + 1
        LenStrPar = 90 - fileData(BegPos) - 1
        BegPos += 1
        StrPar = ret(fileData, BegPos, LenStrPar, Mask)
        ServerSQLPCD.uid = StrPar

        BegPos += LenStrPar + 1
        LenStrPar = 90 - fileData(BegPos) - 1
        BegPos += 1
        StrPar = ret(fileData, BegPos, LenStrPar, Mask)
        ServerSQLPCD.pwd = StrPar
      End If


      If BegPos < fileData.Length Then
        BegPos += LenStrPar + 1
        LenStrPar = 90 - fileData(BegPos) - 1
        BegPos += 1
        StrPar = ret(fileData, BegPos, LenStrPar, Mask)
        ServerFTPFSO.Adress = StrPar

        BegPos += LenStrPar + 1
        LenStrPar = 90 - fileData(BegPos) - 1
        BegPos += 1
        StrPar = ret(fileData, BegPos, LenStrPar, Mask)
        ServerFTPFSO.Port = StrPar

        BegPos += LenStrPar + 1
        LenStrPar = 90 - fileData(BegPos) - 1
        BegPos += 1
        StrPar = ret(fileData, BegPos, LenStrPar, Mask)
        ServerFTPFSO.UserNameWrite = StrPar

        BegPos += LenStrPar + 1
        LenStrPar = 90 - fileData(BegPos) - 1
        BegPos += 1
        StrPar = ret(fileData, BegPos, LenStrPar, Mask)
        ServerFTPFSO.PasswordWrite = StrPar

        BegPos += LenStrPar + 1
        LenStrPar = 90 - fileData(BegPos) - 1
        BegPos += 1
        StrPar = ret(fileData, BegPos, LenStrPar, Mask)
        ServerFTPFSO.UserNameRead = StrPar

        BegPos += LenStrPar + 1
        LenStrPar = 90 - fileData(BegPos) - 1
        BegPos += 1
        StrPar = ret(fileData, BegPos, LenStrPar, Mask)
        ServerFTPFSO.PasswordRead = StrPar

        BegPos += LenStrPar + 1
        LenStrPar = 90 - fileData(BegPos) - 1
        BegPos += 1
        StrPar = ret(fileData, BegPos, LenStrPar, Mask)
        ServerFTPFR.Adress = StrPar

        BegPos += LenStrPar + 1
        LenStrPar = 90 - fileData(BegPos) - 1
        BegPos += 1
        StrPar = ret(fileData, BegPos, LenStrPar, Mask)
        ServerFTPFR.Port = StrPar

        BegPos += LenStrPar + 1
        LenStrPar = 90 - fileData(BegPos) - 1
        BegPos += 1
        StrPar = ret(fileData, BegPos, LenStrPar, Mask)
        ServerFTPFR.UserNameWrite = StrPar

        BegPos += LenStrPar + 1
        LenStrPar = 90 - fileData(BegPos) - 1
        BegPos += 1
        StrPar = ret(fileData, BegPos, LenStrPar, Mask)
        ServerFTPFR.PasswordWrite = StrPar

        BegPos += LenStrPar + 1
        LenStrPar = 90 - fileData(BegPos) - 1
        BegPos += 1
        StrPar = ret(fileData, BegPos, LenStrPar, Mask)
        ServerFTPFR.UserNameRead = StrPar

        BegPos += LenStrPar + 1
        LenStrPar = 90 - fileData(BegPos) - 1
        BegPos += 1
        StrPar = ret(fileData, BegPos, LenStrPar, Mask)
        ServerFTPFR.PasswordRead = StrPar
      End If

    Catch ex As Exception

      UI.MsgErr("Ошибка считывания параметров из файла конфигурации", ex)
      flLoadConfig = False

    Finally

      'флаг загрузки параметров
      flLoadConfig = True

    End Try

  End Sub


  'VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
  'TODO: Процедуру записи конфига
  'VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV


  ''' <summary>
  ''' Расшифровка параметров конфигурации
  ''' </summary>
  ''' <param name="fileData">массив байтов</param>
  ''' <param name="BegPos">начальная позиция</param>
  ''' <param name="LenParam">длина параметра</param>
  ''' <param name="psw">маска (соль)</param>
  ''' <returns>Расшифрованный параметр</returns>
  Private Function ret(ByVal fileData As Byte(), ByVal BegPos As Integer, ByVal LenParam As Integer, ByVal psw As String) As String

    Dim Res As String = ""
    Dim yy As Int32

    ' создаем цикличный ключ
    While psw.Length <= LenParam
      psw += psw
    End While

    Dim ascii As New ASCIIEncoding()

    Dim PswByte As Byte() = ascii.GetBytes(psw)
    Dim tmpbyte As Byte() = New Byte(LenParam) {}

    For i As Integer = 0 To LenParam
      yy = (fileData(i + BegPos) + 256) - PswByte(i)
      tmpbyte(i) = Convert.ToByte(yy Mod 256)
    Next

    Res = ascii.GetString(tmpbyte)

    Return Res

  End Function


  ''' <summary>
  ''' Шифрование параметров конфигурации
  ''' </summary>
  ''' <param name="text">строка для шифрования</param>
  ''' <param name="password">маска (соль)</param>
  ''' <returns>Зашифрованный параметр</returns>
  Private Function Code(ByVal text As String, ByVal password As String) As String

    Dim Res As String = Nothing
    Dim yy As Int32 = 0

    ' создаем цикличный ключ
    While password.Length < text.Length
      password += password
    End While

    Dim enc As Encoding = Encoding.GetEncoding(1251)
    Dim arrByte As Byte() = New Byte(text.Length - 1) {}

    For i As Integer = 0 To text.Length - 1
      yy = Convert.ToInt32(enc.GetBytes(text.Substring(i, 1).ToCharArray(), 0, 1)(0)) + 256 + Convert.ToInt32(enc.GetBytes(password.Substring(i, 1).ToCharArray(), 0, 1)(0))
      arrByte(i) = Convert.ToByte(yy Mod 256)
    Next

    Res = enc.GetString(arrByte, 0, arrByte.Length)

    Return Res

  End Function


End Class
